﻿using DA.Core.Common;
using Microsoft.VisualBasic;
using Prism.Ioc;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DA.Core.Plcs
{
    public class PlcManager
    {
        private Configurations configurations;
        IContainerProvider container;

        private Dictionary<string, IBasePlc> plcDics = new Dictionary<string, IBasePlc>(); 

        public Dictionary<string,IBasePlc> PlcDics
        {
            get { return plcDics; }
            set { plcDics = value; }
        }

        //private List<IPlc> plcList = new List<IPlc>();
        //public List<IPlc> PlcList
        //{
        //    get { return plcList; }
        //    set { plcList = value; }
        //}

        public PlcManager(IContainerProvider container, Configurations configurations)
        {
            this.container = container;
            this.configurations = configurations;
        }
        public void Init()
        {
            configurations?.Appsetting?.Plcs.ForEach(p =>
            {
                var plc = container.Resolve<IBasePlc>();
                plc.Connect(p.IpAddress, p.Port);
                HeartBeat(plc);
                plcDics.Add(p.PlcID,plc);
            });
        }
      
        public void HeartBeat(IBasePlc plc)
        {
            Task.Run(async() =>
            {
                while (true)
                {
                    await Task.Delay(1000);
                    //心跳逻辑

                }
            });
        }
    }
}
